﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Team4Mock.DAO;
using System.Net.Mail;
using System.Net;
namespace Team4Mock.Entity
{
    public class UserEntity
    {
        //Gọi lớp UserDAO ra để sử dụng
        UserDAO ud = new UserDAO();
        private int userid;
        private static string Account;
        private int ques;
        private string answer;
        private static string Password;
        private static string Email;
        private static string Role;

        public string ACCOUNT
        {
            get { return Account; }
            set { Account = value; }
        }

        public string PASSWORD
        {
            get { return Password; }
            set { Password = value; }
        }
        public int Question
        {
            get { return ques; }
            set { ques = value; }
        }
        public string Answer
        {
            get { return answer; }
            set { answer = value; }
        }
        public string EMAIL
        {
            get { return Email; }
            set { Email = value; }
        }
        public int UserID
        { get { return userid; }
            set { userid = value; }
        }
        public string ROLE
        {
            get { return Role; }
            set { Role = value; }
        }
        public UserEntity()
        {
            
        }
        public void Insert(UserEntity e)
        {
            ud.Insert(e.ACCOUNT,VerifyPassword(e),e.EMAIL,e.Question,e.Answer,e.ROLE);
        }
        public void Update(UserEntity e)
        {
            ud.Update(e.UserID,e.ROLE);
        }
        /// <summary>
        /// Kiem tra user da ton tai
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public bool CheckUser(UserEntity e)
        {
            if (ud.CheckUser(e.ACCOUNT) == true)
                return true;
            else return false;
        }
        public bool CheckLogin(UserEntity w)
        {
            if (ud.CheckLogin(w.ACCOUNT, VerifyPassword(w)) == true)
                return true;
            else return false;
        }
        /// <summary>
        /// check email quen pass
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public bool CheckEmail(UserEntity w)
        {
            if (ud.CheckEmail(w.ACCOUNT,w.EMAIL) == true)
                return true;
            else return false;
        }
        public DataTable GetdataByID(UserEntity e)
        {
            return ud.GetdataByID(e.UserID);
        }
        public DataTable GetUser()
        {
            return ud.GetUser();
        }
        public string GetRole(UserEntity v)
        {
            return ud.GetRole(v.ACCOUNT);
        }
        public string VerifyPassword(UserEntity u)
        {
            return ud.VerifyPass(u.PASSWORD);
        }
        public bool SendMail_Thankyou(UserEntity w)
        {
            try
            {

                string to = w.EMAIL;
                int port = 587;
                string subject = "Subbjec";
                string content = "Passsword laf : 84543584365843";
                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Port = port;
                client.Host = "smtp.gmail.com";
                client.Credentials = new NetworkCredential("heineken236@gmail.com", "password");
                MailAddress from = new MailAddress("heineken236@gmail.com", "Restore password");
                MailAddress toAddress = new MailAddress(to);
                MailMessage message = new MailMessage(from, toAddress);
                message.Body = content;
                message.Subject = subject;

                client.Send(message);

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}